package edu.osumc.bmi.restful.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.osumc.bmi.restful.schema.User;
import edu.osumc.bmi.restful.service.RestfulService;
import edu.osumc.bmi.restful.util.RestfulException;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	@Qualifier("restfulService")
	private RestfulService restfulService;

	@RequestMapping(method = RequestMethod.GET, value = "/")
	public ModelAndView getAllUsers() {
		List<User> users = restfulService.getAllUsers();
		ModelAndView mav = new ModelAndView("userXmlView",
				BindingResult.MODEL_KEY_PREFIX + "allUsers", users);
		return mav;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/{userId}")
	public ModelAndView getUser(@PathVariable("userId") Integer id)
			throws RestfulException {
		User user = restfulService.getUser(id);
		ModelAndView mav = new ModelAndView("userXmlView",
				BindingResult.MODEL_KEY_PREFIX + "user", user);
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/{userId}/{firstName}/{lastName}/{loginId}")
	public ModelAndView addUser(@PathVariable("userId") Integer id,
			@PathVariable String firstName, @PathVariable String lastName,
			@PathVariable String loginId) throws RestfulException {
		User user = new User();
		user.setId(id);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setLoginId(loginId);
		restfulService.addUser(user);
		ModelAndView mav = new ModelAndView("userXmlView",
				BindingResult.MODEL_KEY_PREFIX + "user", user);
		return mav;
	}

	@RequestMapping(method = RequestMethod.DELETE, value = "/{userId}")
	public ModelAndView deleteUser(@PathVariable("userId") Integer id)
			throws RestfulException {
		restfulService.deleteUser(id);
		List<User> users = restfulService.getAllUsers();
		ModelAndView mav = new ModelAndView("userXmlView",
				BindingResult.MODEL_KEY_PREFIX + "allUsers", users);
		return mav;
	}

	@RequestMapping(method = RequestMethod.PUT, value = "/{userId}/{firstName}/{lastName}/{loginId}")
	public ModelAndView updateUser(@PathVariable("userId") Integer id,
			@PathVariable String firstName, @PathVariable String lastName,
			@PathVariable String loginId) throws RestfulException {
		User user = restfulService.getUser(id);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setLoginId(loginId);
		restfulService.updateUser(user);
		user = restfulService.getUser(id);
		ModelAndView mav = new ModelAndView("userXmlView",
				BindingResult.MODEL_KEY_PREFIX + "user", user);
		return mav;
	}

}
